#pragma once 

#include <iostream>
#include <pthread.h>
#include <string.h>
#include "Mutex.hpp"

namespace CondModule
{
    using namespace MutexModule;

    class Cond
    {
    public:
        Cond()
        {
            int n = pthread_cond_init(&_cond, nullptr);
            if (n != 0)
            {
                std::cout << "Init error: " << strerror(n) << std::endl;
            }
        }
        void Wait(Mutex &lock)
        {
            int n = pthread_cond_wait(&_cond, lock.LockPtr());
            if (n != 0)
            {
                std::cout << "Wait error: " << strerror(n) << std::endl;
            }
        }
        void Notify()
        {
            int n = pthread_cond_signal(&_cond);
            if (n != 0)
            {
                std::cout << "Signal error: " << strerror(n) << std::endl;
            }
        }
        void NotifyAll()
        {
            int n = pthread_cond_broadcast(&_cond);
            if (n != 0)
            {
                std::cout << "Signal all error: " << strerror(n) << std::endl;
            }
        }
        ~Cond()
        {
            int n = pthread_cond_destroy(&_cond);
            if (n != 0)
            {
                std::cout << "Destroy error: " << strerror(n) << std::endl;
            }
        }
    private:
        pthread_cond_t _cond;
    };
}